<?php
if (!defined("__WD__")) exit("Wrong Connect");

class __Error {

	private $errq = array();

	//http://php.net/manual/en/errorfunc.constants.php
	private $type = array(
		E_ERROR => "Fatal Error",
		E_WARNING => "Warning",
		E_PARSE => "Parser Error",
		E_NOTICE => "Notice",
		E_STRICT => "Strict"
	);
	
	private $reporting = null;
	private $is_show = false;
	
	function __construct() {
		$this->reporting = E_ERROR | E_WARNING | E_PARSE ;
		if (__DEBUG__) $this->reporting = E_ALL | E_STRICT;
	}
	function callNormal( $type, $message, $file, $line) {
		if (!($this->reporting & $type) ) return;
	
		array_push($this->errq, array(
			"type"		=> (isset($this->type[$type]))?$this->type[$type]:$type,
			"message"	=> $message,
			"file"		=> $file,
			"line"		=> $line,
			"trace"		=> debug_backtrace()
		));
	}
	function callException( $e ) {
		array_push($this->errq, array(
			"type"		=> "Exception",
			"message"	=> $e->getMessage(),
			"file"		=> $e->getFile(),
			"line"		=> $e->getLine()
		));
		$this->show(true);
	}
	function callShutdown() {
		$error = error_get_last();
		if ( !is_array($error) ) return;

		ob_get_clean();

		$type		= $error['type'];
		$message	= $error['message'];
		$file		= $error['file'];
		$line		= $error['line'];

        array_push($this->errq, array(
 			"type"		=> (isset($this->type[$type]))?$this->type[$type]:$type,
			"message"	=> $message,
			"file"		=> $file,
			"line"		=> $line,
        ));
		$this->show();        		
	}
	function show( $strict = false ) {

		if ( count($this->errq) == 0 ) return;

		if ( $this->is_show && !$strict ) return;
		if ( !$strict ) $this->is_show = true;
		
		echo <<<CSS
<style>
div.error {margin:10px;border:1px solid #777;border-radius:5px;padding:10px;background:#eee;font-size:12px;}
div.error > *,
div.error > div.trace > * {border:0;margin:0;padding:0;}
div.error > h1 {font-size:16px;line-height:18px;font-weight:bold;}
div.error > p {font-size:12px;}
div.error > div.at {font-size:12px;color:#888;}
div.error > div.trace {font-size:12px;color:#888;}
div.error > div.trace > h2 {font-size:14px;line-height:16px;color:#333}
div.error > div.trace > div.item {font-size:12px;color:#666}
</style>

CSS;
		$hd = "<div class=\"error\">\r\n";
		$ft = "</div>\r\n";
		foreach( $this->errq as $v ) {
			$con = "	<h1>{$v['type']}</h1>\r\n";
			$con .= "	<p>{$v['message']}</p>\r\n";
			$con .= "	<div class=\"at\">{$v['file']}({$v['line']})</div>\r\n";
			if (isset($v['trace'])) {
				$con .= "	<div class=\"trace\">\r\n		<h2>trace</h2>\r\n";
				$trace = $v['trace'];
				$count = count($trace);
				for( $i = 0; $i < $count; $i++ ) {
					if ( !isset($trace[$i]['file']) ) continue;
					$con .= "		<div class=\"item\">{$trace[$i]['file']}({$trace[$i]['line']})</div>\r\n";
				}
				$con .= "	</div>\r\n";
			}
			echo $hd.$con.$ft;
		}

	}
}


class Error extends Exception {

	function show() {
		$err = new __Error();
		$err->callException( $this );
	}
	
}


class __Data {
	
	function __construct() {
	}

	function &make( $name, $mode = 0 ) { // 0 : errorreporting, 1 : overriding
		if ( isset($this->$name) ) {
			if ( $this->$name instanceof __Data ) return;
			else {
				if ($mode === 0) {
					try { throw new Error("이미 사용중인 이름입니다."); }
					catch (Error $e) {echo $e; exit;}
				}
			}
		}
		$this->$name = new __Data();
		return $this->$name;
	}
} // for global data.

class __Controller {

	protected $load = null;
	
	function __construct() {
		$this->load = &getInstance( '__Load' );
		$this->data = &getInstance( '__Data' );
		
		$this->load->config("wandu", $config);
		$this->data->make("config");
		$this->data->config->wandu = $config;

		$this->data->library	= "http://".HOME."/library";

	}

}


class __Model {

	protected $load = null;
	
	function __construct() {
		$this->load = &getInstance( '__Load' );
		$this->data = &getInstance( '__Data' );
	}
	

}

class __Load {
	
	function __construct() {

		$this->data = &getInstance( '__Data' );
		$this->load = &$this; // in view use load.

	}
	function package( $name ) {
		try {
			$url = __APP__."/package/".$name.".php";
			if ( !file_exists( $url ) )  throw new Error("helper파일의 위치가 잘못 지정되어있습니다. (Path : {$url})");
			
			require_once( $url );

			return true;
		}
		catch(Error $e) {
			$e->show();
			return false;
		}

	}
	function view( $name, $data = array() ) {
		try {
			$url = __APP__."/view/".$name.".php";
			if ( !file_exists( $url) ) throw new Error("view파일의 위치가 잘못 지정되어있습니다. (Path : {$url})");

			extract( get_object_vars( $this->data ) );
			if (is_array($data)) extract( $data );
			
			include $url;
			
			return true;
		}
		catch (Error $e) {
			$e->show();
			return false;
		}
	}
	function config( $name, &$ret = null ) {

		try {

			$url = __APP__."/config/".$name.".php";
			if ( !file_exists( $url ) ) throw new Error("config파일의 위치가 잘못 지정되어있습니다. (Path : {$url})");
		
			include $url;
			
			$ret = $config;

			return true;
		}
		catch(Error $e) {
			$e->show();
			return false;
		}
		
	}
	function model( $name, &$ret = null ) {
		
		static $instance;
		
		if ( !isset($instance[$name]) ) {

			try {

				$url = __APP__."/model/".$name.".php";
				if ( !file_exists( $url ) ) throw new Error("model파일의 위치가 잘못 지정되어있습니다. (Path : {$url})");
			
				require_once( $url );
				
				$exp = explode("/", $name);
				$cls_name = "Model_".caseChange("underbar", "pascal", array_pop($exp));
				
				if ( !class_exists( $cls_name ) ) throw new Error("해당 모델의 클래스가 존재하지 않습니다. (Class Name : {$cls_name})");
				$instance[$name] = new $cls_name;
				
			}
			catch(Error $e) {
				$e->show();
				return false;
			}
		
		}

		$ret = $instance[$name];
	
		return true;
	}
	function module( $name, &$ret = null ) {

		static $instance;
		
		if ( !isset($instance[$name]) ) {

			try {

				$url = __SYSTEM__."/module/".$name.".php";
				if ( !file_exists( $url ) ) throw new Error("module파일의 위치가 잘못 지정되어있습니다. (Path : {$url})");
			
				require_once( $url );
				
				$exp = explode("/", $name);
				$cls_name = "Module_".caseChange("underbar", "pascal", array_pop($exp));
				
				if ( !class_exists( $cls_name ) ) throw new Error("해당 모듈의 클래스가 존재하지 않습니다. (Class Name : {$cls_name})");
				$instance[$name] = new $cls_name;

			}
			catch(Error $e) {
				$e->show();
				return false;
			}
		
		}

		$ret = $instance[$name];

		return true;

	}
	function helper( $name ) {

		static $instance;
		
		if ( !isset($instance[$name]) ) {
			try {
				
				$instance[$name] = "success";
				
				$url = __SYSTEM__."/helper/".$name.".php";
				if ( !file_exists( $url ) )  throw new Error("helper파일의 위치가 잘못 지정되어있습니다. (Path : {$url})");
				
				require_once( $url );
	
				return true;
			}
			catch(Error $e) {
				$e->show();
				return false;
			}
		}
		return true;

	}
	
	function err() {
		$err = &getInstance("__Error");
		$err->show();
	}

	

}



class __Module {
	
	protected $load = null;
	
	function __construct() {
		$this->load = &getInstance( '__Load' );
		$this->data = &getInstance( '__Data' );
	}

}





























// End